﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace SPDesigner.Policy
{
     internal class SPDAdminLogic
    {
         internal static void SetSPSiteSPD(SPSite site, bool enable)
        {

            var web = site.RootWeb;
            SetSPWebSPD(web, enable);
            foreach (SPWeb subweb in web.Webs)
            {
                SetSPWebSPD(subweb, enable);
                web.Dispose();
            }
            site.RootWeb.Dispose();


        }

         internal static void SetSPWebSPD(SPWeb web, bool enable)
        {
            if (!enable)
            {
                if (web.AllProperties.ContainsKey("vti_disablewebdesignfeatures2") && web.AllProperties["vti_disablewebdesignfeatures2"].Equals("wdfopensite"))
                    return;
                else
                {
                    web.AllowUnsafeUpdates = true;
                    web.AllProperties.Add("vti_disablewebdesignfeatures2", "wdfopensite");
                    web.Update();
                    return;
                }

            }
            else
                if (web.AllProperties.ContainsKey("vti_disablewebdesignfeatures2") && web.AllProperties["vti_disablewebdesignfeatures2"].Equals("wdfopensite"))
                {
                    web.AllowUnsafeUpdates = true;
                    web.AllProperties.Remove("vti_disablewebdesignfeatures2");
                    web.Update();
                    return;

                }

        }
    }
}
